package zf.proxy;

import org.springframework.cglib.proxy.Enhancer;
import zf.services.FangDongServices;
import zf.services.impl.FangDongServicesImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class DynamicZuFangProxy {
    public void zuFang(){
        //接口的支持 用于保存业务的一致性
        final FangDongServices fds = new FangDongServicesImpl();
        //创建一个动态代理
        InvocationHandler in = new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //辅助功能
                System.out.println("发布房源");
                System.out.println("带入看房");
                System.out.println("房子介绍");
                //核心功能
                fds.zuFang();
                return null;
            }
        };
        //动态代理类的生成
        //参数1 动态代理类的本类的claslLoader，参数2 核心业务的接口数组 参数3 动态代理对象
        //返回出来的这个object就是生成动态代理类
//        Object o = Proxy.newProxyInstance(DynamicZuFangProxy.class.getClassLoader(), fds.getClass().getInterfaces(), in);
        FangDongServices o = (FangDongServices) Proxy.newProxyInstance(DynamicZuFangProxy.class.getClassLoader(), fds.getClass().getInterfaces(), in);
        //调用代理类
        System.out.println(o.getClass());
        o.zuFang();
    }

    /**
     * 生成动态代理 ==> 辅助功能日志功能
     * @param target
     * @return
     */
//    public Object getProxy(final Object target){
//        InvocationHandler in = new InvocationHandler() {
//            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//                System.out.println("开始日志的记录");
//                System.out.println("方法名"+method.getName());
//                System.out.println("方法的参数"+args);
//                System.out.println(target.getClass().getSimpleName());
//                return null;
//            }
//        };
//        return Proxy.newProxyInstance(DynamicZuFangProxy.class.getClassLoader(),target.getClass().getInterfaces(),in);
//    }

    public Object getProxy(final Object target){
        InvocationHandler in = new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //辅助功能
                System.out.println("开始日志的记录");
                System.out.println("方法名"+method.getName());
                System.out.println("方法的参数"+args);
                System.out.println(target.getClass().getSimpleName());
                //使用java反射，调用核心方法/核心功能
                Object invoke = method.invoke(target, args);
                return invoke;
            }
        };
        return Proxy.newProxyInstance(DynamicZuFangProxy.class.getClassLoader(),target.getClass().getInterfaces(),in);
    }

    public void cgLibZuFang(){
        Enhancer en = new Enhancer();
        final FangDongServices fds = new FangDongServicesImpl();
        //普通类
        en.setSuperclass(fds.getClass());
        en.setCallback(new org.springframework.cglib.proxy.InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] args) throws Throwable {
                //辅助功能
                System.out.println("开始日志的记录");
                System.out.println("方法名" + method.getName());
                System.out.println("方法的参数" + args);
                //调用核心功能
                fds.zuFang();
                return null;
            }
        });

        FangDongServices o = (FangDongServices)en.create();
        System.out.println(o.getClass());
        o.zuFang();
    }

}
